package com.tcgroup.phone.base_core.framework.mvvm;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.tcgroup.phone.base_core.R;
import com.tcgroup.phone.base_core.event.event_bus.BaseActivityEvent;
import com.tcgroup.phone.base_core.framework.IBaseView;
import com.tcgroup.phone.base_core.loadsir.EmptyCallback;
import com.tcgroup.phone.base_core.loadsir.ErrorCallback;
import com.tcgroup.phone.base_core.loadsir.LoadingTransCallback;
import com.tcgroup.phone.base_core.manager.ActivityManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/16.
 * 包名: com.tcgroup.phone.base_core.framework.mvvm
 * 类名: HibeautyNew-Android
 * Container
 */
public abstract class AbstractActivity extends AppCompatActivity implements IBaseView {


    protected LoadService mLoadService;
    // 提供了视图的根元素，是一个FrameLayout
    // 1.可以获得获取导航栏高度
    //2. 获取键盘高度
    //3.那怎么获取状态栏的高度
    protected ViewGroup mRootContentView;
    protected Toolbar mToolbar;
    private ViewStub mViewStubToolbar;
    private RelativeLayout mViewStubContent;
    private TextView mToolbarTitle;
    protected View mTopView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.core_activity_root_01);
        mRootContentView = findViewById(android.R.id.content);
        initCommonView();
        initContentView();
        ARouter.getInstance().inject(this);
        initView();
        initListener();
        initData();
        EventBus.getDefault().register(this);
        ActivityManager.getInstance().addActivity(this);
    }


    private void initCommonView(){
        mViewStubToolbar = findViewById(R.id.core_view_stub_toolbar);
        mViewStubContent = findViewById(R.id.core_view_stub_content);
        mTopView = findViewById(R.id.top_view);
        if (enableToolbar()){
            mViewStubToolbar.setLayoutResource(onBindToolbarLayout());
            View view = mViewStubToolbar.inflate();
            initToolbar(view);
        }
    }

    protected void initToolbar(View view){
        initImmersionBar(null);
        mToolbar = view.findViewById(R.id.core_toolbar_root);
        mToolbarTitle = view.findViewById(R.id.core_toolbar_title);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(enable_TB_BackImage());
        getSupportActionBar().setDisplayShowTitleEnabled(false);//设置actionbar标题不显示
        mToolbar.setNavigationOnClickListener(v -> {
            onBackPressedClick();
            finishActivity();
        });
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        if (null != mViewStubContent){
            initContentView(layoutResID);
        }
    }

    public void initContentView(){
        initContentView(onBindLayout());
    }

    private void initContentView(@LayoutRes int layoutResID){
        View view = LayoutInflater.from(this).inflate(layoutResID,mViewStubContent,false);
        mViewStubContent.setId(android.R.id.content);
        mRootContentView.setId(View.NO_ID);
        mViewStubContent.removeAllViews();
        mViewStubContent.addView(view);
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        if (null != mToolbarTitle && !TextUtils.isEmpty(title)){
            mToolbarTitle.setText(title);
        }
        //通过getTootBarTitle方法来覆盖
        String tootBarTitle = getTootBarTitle();
        if (null != mToolbarTitle && !TextUtils.isEmpty(tootBarTitle)) {
            mToolbarTitle.setText(tootBarTitle);
        }
    }

    @Override
    public void initView() { }

    @Override
    public void initListener() { }

    @Override
    public void initData() { }

    @Override
    public void finishActivity() { finish(); }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        ActivityManager.getInstance().finishActivity(this);

    }

    protected void binding_LoadSir(){
        mLoadService = LoadSir.getDefault().register(bindLoadSir(), (Callback.OnReloadListener) v -> {
            if (v.getId() == R.id.core_rl_no_data_root){
                onRetryBtnClick();
                return;
            }
            if (v.getId() == R.id.core_rl_net_error_root){//点击可以跳转查看网络？
                onRetryBtnClick();
                return;
            }
        });
    }

    @Override
    public void showLoading(){
        if (mLoadService == null){
            binding_LoadSir();
        }
        mLoadService.showCallback(LoadingTransCallback.class);
    }

    @Override
    public void showContent(){
        if (null == mLoadService){
            binding_LoadSir();
        }
        mLoadService.showSuccess();
    }

    @Override
    public void showEmpty(){
        if (null == mLoadService){
            binding_LoadSir();
        }
        mLoadService.showCallback(EmptyCallback.class);
    }

    @Override
    public void showError(){
        if (null == mLoadService){
            binding_LoadSir();
        }
        mLoadService.showCallback(ErrorCallback.class);
    }

    public boolean enableToolbar(){
        return true;
    }

    public boolean enable_TB_BackImage(){
        return true;
    }

    public int onBindToolbarLayout() {
        return R.layout.core_common_toolbar;
    }

    public String getTootBarTitle() {
        return "";
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public <T> void onEvent(BaseActivityEvent<T> event) { }

    /**
     * 初始化沉浸式
     * Init immersion bar.
     */
    public void initImmersionBar(View view) {
        ImmersionBar.with(this)
                .statusBarView(R.id.top_view)
                .statusBarColor(R.color.colorPrimary)
                .navigationBarColor(R.color.colorPrimary)
                .titleBar(mToolbar)
                .init();
    }

    /**
     * 重新加载数据
     */
    public abstract void onRetryBtnClick();

    public abstract void onBackPressedClick();

    public abstract int onBindLayout();

    public abstract Object bindLoadSir();

}
